using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Bll;
using Entities;

public partial class signup : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnCreateMyAccount_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            if (ViewState["taxcode"] != null)
            {
                InsertAccount("dealer");
                InsertCustomer(txtTaxCode.Text);
                Response.Redirect("registersuccess.aspx");
            }
            else
            {
                InsertAccount("customer");
                InsertCustomer("0");
                Response.Redirect("registersuccess.aspx");
            }
        }

    }

    public void InsertAccount(string clientRole)
    {
        AccountBll accBll = new AccountBll();
        AccountInfo accInfo = new AccountInfo();
        accInfo.Username = txtUsername.Text;
        accInfo.Password = txtPassword.Text;
        accInfo.SecretQuestion = ddlSecretQuestion.SelectedValue;
        accInfo.SecretAnswer = txtAnswer.Text;
        accInfo.Role = clientRole;
        accBll.Insert(accInfo);
    }

    public void InsertCustomer(string taxcode)
    {
        CustomerBll custBll = new CustomerBll();
        CustomerInfo custInfo = new CustomerInfo();
        custInfo.UserName = txtUsername.Text;
        custInfo.FirstName = txtFirstName.Text;
        custInfo.LastName = txtLastName.Text;
        custInfo.DateOfBirth = DateTime.Parse(txtDateOfBirth.Text);
        custInfo.Email = txtEmail.Text;
        custInfo.Address = txtAddress.Text;
        custInfo.City = txtCity.Text;
        custInfo.Phone = txtPhone.Text;
        custInfo.BankName = ddlstBankName.SelectedValue;
        custInfo.AccountNumber = txtAccountNumber.Text;
        custInfo.TaxCode = taxcode;
        custBll.Insert(custInfo);
    }

    protected void lbDealer_Click(object sender, EventArgs e)
    {
        lblTaxCode.Visible = true;
        lblTaxCode.Text = "Tax Code  : ";
        txtTaxCode.Visible = true;
        ViewState["taxcode"] = "enable";

    }
}
